package com.avocado.base.controller;


import com.avocado.base.dto.role.RoleAddDto;
import com.avocado.base.dto.role.RoleEditDto;
import com.avocado.base.dto.role.RoleQueryDto;
import com.avocado.base.service.RoleService;
import com.avocado.commons.model.http.BaseResponse;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;




@Controller
@RequestMapping("/role/*")
public class RoleController {
	

	private final RoleService roleService;

	public RoleController(RoleService roleService) {
		this.roleService = roleService;
	}

	 @RequestMapping("/getOne")
     public @ResponseBody BaseResponse getOne(Long id) {

		return BaseResponse.builder().status(1).data(roleService.getOne(id)).build();
     }

	 @PreAuthorize("hasAnyAuthority('ROLE_ADD')")
	 @RequestMapping("/add")
	 public @ResponseBody BaseResponse add(@RequestBody @Validated RoleAddDto t) {

		 long id=roleService.add(t);

		 return BaseResponse.builder().status(1).data(id).message("操作成功!").build();
    }

	 @PreAuthorize("hasAnyAuthority('ROLE_UPDATE')")
	 @RequestMapping("/update")
     public @ResponseBody BaseResponse update(@RequestBody @Validated RoleEditDto t){

		 roleService.update(t);

		 return BaseResponse.builder().status(1).message("操作成功!").build();
     }

	 @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
	 @RequestMapping("/findByPage")
     public @ResponseBody BaseResponse findByPage(@PageableDefault Pageable pageable,RoleQueryDto dto) {

		 return BaseResponse.builder().status(1).data(roleService.findByPage(pageable, dto)).build();
     }
    
	 @RequestMapping("/findAll")
     public @ResponseBody BaseResponse findAll(RoleQueryDto dto) {

		 return BaseResponse.builder().status(1).data(roleService.findAll(dto)).build();
     }

	 @PreAuthorize("hasAnyAuthority('ROLE_DELETE')")
	 @RequestMapping("/deleteById")
	 //清空用户菜单缓存
	 @CacheEvict(value="userMenu", allEntries=true)
     public @ResponseBody BaseResponse deleteById(Long id) {

		 int status=roleService.deleteById(id)==Boolean.TRUE?1:0;

		 return BaseResponse.builder().status(status).message("操作成功!").build();
     }
	 
	 @RequestMapping("/findAllByCategory")
     public @ResponseBody BaseResponse findAllByCategory(String  category) {

		 return BaseResponse.builder().status(1).data(roleService.findAllByCategory(category)).build();
     }
	 
	 @RequestMapping("/findByCode")
     public @ResponseBody BaseResponse findByCode(String  code) {

		 return BaseResponse.builder().status(1).data(roleService.findByCode(code)).build();
     }


	@RequestMapping("/findAllRoleForUser")
	public @ResponseBody BaseResponse findAllRoleForUser(Long userId) {

		return BaseResponse.builder().status(1).data(roleService.findAllRoleForUser(userId)).build();
	}
	

}
